package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.ceramics.data.Order;
import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;
import com.example.ceramics.entity.CartEntity;
import com.example.ceramics.entity.GoodsEntity;
import com.example.ceramics.entity.OrderEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements IOrderDao {

    private final DatabaseHelper databaseHelper;

    public OrderDaoImpl(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    @Override
    public int addOrder(int goodsId, String account, double goodsPrice) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "insert into "+ DatabaseContants.TABLE_ORDER+"(account,goodsId,num,price,time) values(?,?,?,?,?,?)";

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String time = sdf.format(date);
//        System.out.println("现在时间：" + sdf.format(date)); // 输出已经格式化的现在时间（24小时制）

        db.execSQL(sql,new Object[]{account,goodsId,1,goodsPrice,time});
        db.close();
        return 0;
    }

    @Override
    public int addOrder(List<CartEntity> cartList) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "insert into "+ DatabaseContants.TABLE_ORDER+"(account,goodsId,num,price,time) values(?,?,?,?,?)";

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String time = sdf.format(date);
//        System.out.println("现在时间：" + sdf.format(date)); // 输出已经格式化的现在时间（24小时制）

        for (CartEntity cart : cartList) {
            db.execSQL(sql,new Object[]{cart.getAccount(),cart.getGoodsId(),cart.getNum(),cart.getPrice(),time});
        }
        db.close();
        return 0;
    }

    @Override
    public int deleteOrder(int id) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "delete from "+DatabaseContants.TABLE_ORDER+" where id=?";
        db.execSQL(sql,new Object[]{id});
        db.close();
        return 0;
    }

    @Override
    public boolean hasOrder(int id) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select id from "+DatabaseContants.TABLE_ORDER+" where id=?";
        Cursor cursor = db.rawQuery(sql, new String[]{"" + id});
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return true;
        }
        cursor.close();
        db.close();
        return false;
    }

    @Override
    public List<OrderEntity> getOrderList(String account) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_ORDER+" join "+DatabaseContants.TABLE_GOODS +
                " on " +DatabaseContants.TABLE_ORDER+".goodsId="+DatabaseContants.TABLE_GOODS+".goodsId where account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        List<OrderEntity> orderList = new ArrayList<>();
        while (cursor.moveToNext()) {
            orderList.add(cursorToOrder(cursor));
        }
        cursor.close();
        db.close();
        return orderList;
    }

    private OrderEntity cursorToOrder(Cursor cursor) {
        GoodsEntity goods;
        int goodsId = cursor.getInt(cursor.getColumnIndex("goodsId"));
        String goodsName = cursor.getString(cursor.getColumnIndex("goodsName"));
        double goodsPrice = cursor.getDouble(cursor.getColumnIndex("goodsPrice"));
        String goodsSignature = cursor.getString(cursor.getColumnIndex("goodsSignature"));
        String goodsType = cursor.getString(cursor.getColumnIndex("goodsType"));
        int goodsImg = cursor.getInt(cursor.getColumnIndex("goodsImg"));
        goods = new GoodsEntity(goodsId, goodsName, goodsPrice, goodsSignature, goodsType, goodsImg);

        int id = cursor.getInt(cursor.getColumnIndex("id"));
        String account = cursor.getString(cursor.getColumnIndex("account"));
        int num = cursor.getInt(cursor.getColumnIndex("num"));
        double price = cursor.getDouble(cursor.getColumnIndex("price"));
        String time = cursor.getString(cursor.getColumnIndex("time"));
        OrderEntity order = new OrderEntity(id,account,goodsId,num,price,time,goods);
        return order;
    }
}
